﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Segmentation.Business
{
    static class NormalDistribution
    {
        #region Fields

        private static Random rand = new Random();

        #endregion Fields

        #region Methods

        public static double GetNext()
        {
            double randn = 0;
            /*
            // I
            double X = 0;
            double N = 100;

            for (int j = 0; j < N; j++)
            {
                X += Utils.rand.NextDouble();
            }

            randn = X / N;
            */

            /*
            // II
            randn = Math.Sqrt(-2.0 * Math.Log(1.0 - Utils.rand.NextDouble())) * Math.Cos(6.283185307 * Utils.rand.NextDouble());
            */

            // III
            double V1 = 0;
            double V2 = 0;
            double S = 0;
            do
            {
                V1 = 2 * rand.NextDouble() - 1;
                V2 = 2 * rand.NextDouble() - 1;
                S = V1 * V1 + V2 * V2;
            }
            while (S >= 1);

            randn = Math.Sqrt(-2 * Math.Log(S) / S) * V1;

            return randn;
        }

        public static double GetNext(double variance)
        {
            return Math.Sqrt(variance) * GetNext();
        }

        #endregion
    }
}
